package com.foodost.idiomer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class ExplanInitTask extends AsyncTask<Context, Integer, String> {

    private static final String TAG = "ExplanInitTask";
    private FinalDb fDB;
    private Context mContext;
    
    @Override
    protected String doInBackground(Context... params) {
        mContext = params[0];
        fDB = IdiomModel.getFinalDb(mContext);
        readExcelAndSaveData(mContext);//readOriginData();
        if(mContext instanceof InitDataService){
            Utils.explanInitFilish(mContext);
        }
        return "SUCCESS";
    }
    //do not use
    public List<IdiomExplan> readOriginData(){
        
        List<IdiomExplan> list = new ArrayList<IdiomExplan>();
        
        try {
            InputStream in = mContext.getResources().getAssets().open("explan.txt");
            InputStreamReader is = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(is);
            StringBuilder sb = new StringBuilder();
            String strLine = null;
            while((strLine = br.readLine()) != null){
                sb.append(strLine);
            }
            packageDao(sb.toString(),list,"explan.txt");
            br.close();
            is.close();
            in.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    //do not use
    private void packageDao(String coredata,List<IdiomExplan> list,String fileName){
        String [] idiomExplansArr = coredata.split("##");
        for(String idiomExplans : idiomExplansArr){
            IdiomExplan idiomExplan = new IdiomExplan();
            if(idiomExplans.trim().length() > 0){
                String [] idiomExplanArr = idiomExplans.split("::");
                if(idiomExplanArr.length > 1){
                    idiomExplan.setIdiomRight(idiomExplanArr[0].trim());
                    idiomExplan.setIdiomExplan(idiomExplanArr[1].trim());
                    list.add(idiomExplan);
                }
                
            }
            
        }
    }
    
    public void readExcelAndSaveData(Context mContext){
        InputStream in;
        Set<String> idiomRightSet  = new TreeSet<String>();
        try {
            in = mContext.getResources().getAssets().open("explan.xls");
            Workbook book = Workbook.getWorkbook(in);
            Sheet sheet = book.getSheet("explan");
            int rownum = sheet.getRows();
            int cols = sheet.getColumns();
            Log.d(TAG, "rownum = " + rownum);
            Log.d(TAG, "cols = " + cols);
            for(int row = 0; row < rownum; row++){
                IdiomExplan explan = new IdiomExplan();
                explan.setIdiomRight(sheet.getCell(0, row).getContents());
                explan.setIdiomExplan(sheet.getCell(1, row).getContents());
                int oldSet = idiomRightSet.size();
                idiomRightSet.add(explan.getIdiomRight());
                int newSet = idiomRightSet.size();
                if(newSet > oldSet){//预防重复
                    fDB.save(explan);
                }
            }
            book.close();
        } catch (BiffException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

}
